<template>
  <div class="warp-box" id="upadd">
    <el-row class="search-box">
      <el-col :span="24" class="add-bottom">
        <span class="personal-title" v-text="parames.id?'编辑素材':'新建素材'"></span>
      </el-col>
    </el-row>
    <div class="main-box">
      <div class="inforlist">
        <div class="inforadd" style="margin-top: 30px;width: 880px">
          <el-form
            v-loading="loading"
            :model="parames"
            :rules="rules"
            ref="parames"
            label-width="120px"
            class="demo-ruleForm"
          >
            <el-form-item label="url:" prop="url">
              <el-input style="width:400px;float:left" v-model="parames.url" placeholder="请输入url"></el-input>
              <el-upload
                style="float:left"
                class="upload-demo"
                name="files"
                accept=".zip"
                :action="action"
                :headers="headers"
                :on-success="uploadimg"
                :on-progress="onUpload"
                :on-error="onError"
                :show-file-list="false"
              >
                <span class="search-btn backorgin" style="margin: 0 15px">
                  <i class="element-icons icon-shangchuan" style="font-size:20px;"></i>
                </span>
              </el-upload>
            </el-form-item>
            <el-form-item label="名称:" prop="name">
              <el-input style="width:400px;" v-model="parames.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="版本:" prop="version">
              <el-input style="width:400px;" v-model="parames.version" placeholder="请输入版本"></el-input>
            </el-form-item>
            <el-form-item label="简介:" prop="memo">
              <el-input
                rows="6"
                style="width:400px;"
                type="textarea"
                v-model="parames.memo"
                placeholder="请输入简介"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态:" prop="status">
              <el-select style="width:400px;" v-model="parames.status" placeholder="请选择状态">
                <el-option
                  v-for="item in stateData"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button round type="primary" @click="handleSend">保存并返回列表</el-button>
              <el-button round @click="resetData">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      parames: {
        id: "",
        name: "",
        url: "",
        version: "",
        status: "",
        memo: ""
      },
      stateData: [
        { value: "0", label: "开发" },
        { value: "2", label: "审核" },
        { value: "1", label: "上线" }
      ],
      loading: false,
      action: "",
      headers: {
        "X-Requested-With": "XMLHttpRequest"
      },
      moduleKeys: [],
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        url: [{ required: true, message: "请输入url", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "blur" }]
      }
    };
  },
  methods: {
    uploadimg(response) {
      this.parames.url = response.data[0].src;
      this.loading = false
        if(response.code !== 10200) {
            this.$alert(response.msg, '提示')
        }
    },
    onError (err) {
        this.loading = false
        this.$alert('文件上传失败', '提示')
    },
    onUpload () {
        this.loading = true
    },
    handleSend() {
      let url = "";
      if (this.parames.id) {
        url = "/tx/module/file/edit";
      } else {
        url = "/tx/module/file/save";
      }
      this.$refs.parames.validate(valid => {
        if (valid) {
          this.$axios
            .post(url, {
              id: this.parames.id,
              name: this.parames.name,
              url: this.parames.url,
              version: this.parames.version,
              status: this.parames.status,
              memo: this.parames.memo
            })
            .then(res => {
              if (res.code === 10200) {
                this.resetData();
                this.$router.push({ name: "module_file_list" });
              }
            });
        } else {
          return false;
        }
      });
    },
    render(id) {
      this.$axios.get("/tx/module/file/edit/" + id).then(res => {
        if (res.code === 10200) {
          this.parames = res.data;
          this.parames.status = res.data.status + "";
        }
      });
    },
    resetData() {
      this.parames.url = "";
      this.parames.id = "";
      this.parames.name = "";
      this.parames.version = "";
      this.parames.status = "";
      this.parames.memo = "";
      this.$refs.parames.resetFields();
    }
  },
  mounted() {
    this.parames.id = this.$route.query.id;
    if (this.parames.id) {
      this.render(this.parames.id);
    } else {
    }
    this.action = this.$axios.api + "/tx/upload/module";
  }
};
</script>
<style>
.el-checkbox + .el-checkbox {
  margin: 0;
  padding-right: 30px;
}
</style>
